//
//  DemoViewController.m
//  CairnStory
//  www.cairnstory.com
//
//  Created by saturna on 11. 3. 22..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DemoViewController.h"MHomepy
#import "CSUIAlertView.h"
#import "CSNateOAuthResult.h"
#import "CSNateSessionController.h"
#import "CSNateRequest.h"
#import "CSMultipartAttach.h"
#import "CSXMLObject.h"



/*
1. 미니홈피에 사진첩에 사진 업로드 및 글 게시 Flow
   1) 네이트 로그인
   2) 사진폴더 선택 -- folderNo, miniHomepyId 를 얻는다.
   3) 사진 업로드 및 글작성 confirm
   4) 사진 업로드 -- attachName을 얻는다.
   5) 사진글 게시 -- 게시물 Id (postId)를 얻는다.
   6) 게시물 보여주기 confirm
   7) 게시물 보여주기
2. CLog 노트에 사진 업로드 및 글 게시 Flow
   1) 사진 업로드 및 글작성 confirm
   2) 사진 업로드 -- attachId를 얻는다.
   3) 노트글 게시 - 노트 일련번호 (noteSeq)를 얻는다.
   4) 노트글 보여주기 confirm
   5) 노트글 보여주기
*/
#pragma mark -
#pragma mark DemoViewController (PrivateMethods)

@interface DemoViewController (PrivateMethods)

- (void)updateUI;
- (void)startProgressIndicator;
- (void)stopProgressIndicator;

#pragma mark -
- (void)loginNate;
- (void)logoutNate;
- (void)forceLogoutNate;
- (void)nateLogoutAfterConfirmView:(CSUIAlertView*)alertView buttonAtIndex:(NSUInteger)buttonIndex;

#pragma mark -
- (void)presentMHomepyPhotoFolderSelectView;
- (void)confirmPostToMHomepyPhotoAlbum;
- (void)uploadPhotoToMHomepyPhotoAlbumAfterConfirmView:(CSUIAlertView*)alertView
										 buttonAtIndex:(NSUInteger)buttonIndex;
- (void)uploadPhotoToMHomepyPhotoAlbum;
- (void)postMessageToMHomepyPhotoAlbumWithAttachName:(NSString*)attachName;

- (void)openMHomepyPhotoURLWithConfirmMessage:(NSString*)confirmMessage;
- (void)openMHomepyPhotoURLAfterConfirmView:(CSUIAlertView*)alertView
							  buttonAtIndex:(NSUInteger)buttonIndex;

#pragma mark -
//- (void)requestCLogProfile;
- (void)confirmPostToCLogNote;
- (void)uploadToCLogAfterConfirmView:(CSUIAlertView*)alertView buttonAtIndex:(NSUInteger)buttonIndex;
- (void)uploadPhotoToCLogNote;
- (void)postMessageToCLogNoteWithAttachId:(NSString*)attachId;

- (void)openCLogNoteURLWithConfirmMessage:(NSString*)confirmMessage;
- (void)openCLogNoteURLAfterConfirmView:(CSUIAlertView*)alertView
						  buttonAtIndex:(NSUInteger)buttonIndex;

@end


#pragma mark -

#define kNateTagLogin 1

#define kNateTagPostToMHomepyPhotoAlbum_selectFolder 10
#define kNateTagPostToMHomepyPhotoAlbum_uploadPhoto 11
#define kNateTagPostToMHomepyPhotoAlbum_postMessage 12

#define kNateTagPostToCLogNote_uploadPhoto 20
#define kNateTagPostToCLogNote_postMessage 21

#define kAttributeKeyMHomepyId @"MHomepy.id"
#define kAttributeKeyMHomepyFolderNo @"MHomepy.folderNo"
#define kAttributeKeyMHomepyPhotoPostId @"MHomepy.postId"
#define kAttributeKeyCLogNoteSeq @"CLog.noteSeq"



#pragma mark -
#pragma mark DemoViewController

@implementation DemoViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	m_progressIndicatorView.hidden = YES;
	//m_attribute = [[NSMutableDictionary alloc] init];
	[self updateUI];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload
{
	[super viewDidUnload];
}


- (void)dealloc
{
	[m_statusLabel release];
	[m_progressIndicatorView release];

	[m_loginButton release];
	[m_postToCLogButton release];
	[m_postToPhotoAlbumButton release];

	if (m_nateSessionController != nil)
	{
		[m_nateSessionController release];
		m_nateSessionController = nil;
	}
	if (m_nateRequest != nil)
	{
		[m_nateRequest release];
		m_nateRequest = nil;
	}

	//[m_attribute release];

	[super dealloc];
}

#pragma mark -
#pragma mark button actions

- (IBAction)tapLoginButton:(id)sender
{
	if ([self.appDelegate isNateAuthorized])
	{
		[self logoutNate];
	}
	else
	{
		[self loginNate];
	}
}


- (IBAction)tapPostToPhotoAlbumButton:(id)sender
{
	[self presentMHomepyPhotoFolderSelectView];
}

- (IBAction)tapPostToCLogButton:(id)sender
{
	[self confirmPostToCLogNote];
}




#pragma mark -
#pragma mark CSNateSessionDelegate

- (void)nateSessionController:(CSNateSessionController*)controller
			  didStartWithTag:(NSUInteger)nateTag
{
	// do nothing
}

- (void)nateSessionController:(CSNateSessionController*)controller
			  didLoginWithTag:(NSUInteger)nateTag
			   withAuthResult:(CSNateOAuthResult*)authResult
{
	[self updateUI];
	if (DEBUG) CSLog(@"===> %@", authResult);

	if (kNateTagLogin == nateTag)
	{
		// do nothing
	}
	else if (kNateTagPostToMHomepyPhotoAlbum_selectFolder == nateTag)
	{
		[self presentMHomepyPhotoFolderSelectView];
	}
	else if (kNateTagPostToCLogNote_uploadPhoto == nateTag)
	{
		[self confirmPostToCLogNote];
	}
}

- (void)nateSessionController:(CSNateSessionController*)controller
			 didRejectWithTag:(NSUInteger)nateTag
			   withAuthResult:(CSNateOAuthResult*)authResult
				  withMessage:(NSString*)alertMessage
{
	[self showAlertView:alertMessage];
}

- (void)nateSessionController:(CSNateSessionController*)controller
			 didCancelWithTag:(NSUInteger)nateTag
{
	// do nothing
}

- (void)nateSessionController:(CSNateSessionController*)controller
			   didFailWithTag:(NSUInteger)nateTag
				  withMessage:(NSString*)alertMessage
					withError:(NSError*)error
{
	if (error == nil)
	{
		[self showAlertView:alertMessage];
	}
	else
	{
		[self showAlertView:[alertMessage stringByAppendingFormat:@"\n%@", [error localizedDescription]]];
	}
}

- (void)nateSessionController:(CSNateSessionController*)controller
			 didFinishWithTag:(NSUInteger)nateTag
{
	if (m_nateSessionController != nil)
	{
		[m_nateSessionController release];
		m_nateSessionController = nil;
	}
}



#pragma mark -
#pragma mark CSNateMHomepyFolderSelectViewDelegate

- (void)folderSelectViewController:(CSNateMHomepyFolderSelectViewController*)controller
			 didSelectWithMenuType:(CSNateMHomepyMenuType)menuType
					withFolderInfo:(NSDictionary*)folderInfo
{
	NSDictionary* dic;
	dic = [folderInfo objectForKey:@"id"];
	[self setAttribute:[dic objectForKey:CSXMLObjectTextKey] forKey:kAttributeKeyMHomepyId];

	dic = [folderInfo objectForKey:@"folderNo"];
	[self setAttribute:[dic objectForKey:CSXMLObjectTextKey] forKey:kAttributeKeyMHomepyFolderNo];

	if (controller.modalInPopover)
	{
		[self.navigationController dismissModalViewControllerAnimated:YES];
	}
	else
	{
		[self.navigationController popViewControllerAnimated:YES];
	}

	[self confirmPostToMHomepyPhotoAlbum];
}

- (void)folderSelectViewController:(CSNateMHomepyFolderSelectViewController*)controller
			 didCancelWithMenuType:(CSNateMHomepyMenuType)menuType
{
	if (controller.modalInPopover)
	{
		[self.navigationController dismissModalViewControllerAnimated:YES];
	}
	else
	{
		[self.navigationController popViewControllerAnimated:YES];
	}
}





#pragma mark -
#pragma mark CSNateRequestDelegate


- (void)nateRequest:(CSNateRequest*)request
	didStartWithTag:(NSUInteger)nateTag
{
	[self startProgressIndicator];
}


- (void)nateRequest:(CSNateRequest*)request
  didSuccessWithTag:(NSUInteger)nateTag
	 withOutputType:(CSNateRequestOutputType)outputType
		 withResult:(id)result
{
	if (DEBUG) CSLog(@"===> %@", result);
	if (kNateTagPostToMHomepyPhotoAlbum_uploadPhoto == nateTag)
	{
		NSString* photoAttachName = [((CSXMLObject*)result) subTextWithElementPath:@"/Data/Attach/AttachNm"];
		if (photoAttachName == nil)
		{
			[self showAlertView:@"Mini Homepy photo post failed!!"];
		}
		[self postMessageToMHomepyPhotoAlbumWithAttachName:photoAttachName];
	}
	else if (kNateTagPostToMHomepyPhotoAlbum_postMessage == nateTag)
	{
		NSString* postId = [((CSXMLObject*)result) subTextWithElementPath:@"/ArrayOfint/int"];
		if ([postId intValue] <= 0)
		{
			[self showAlertView:@"Mini Homepy photo post failed!!!"];
			return;
		}

		[self setAttribute:postId forKey:kAttributeKeyMHomepyPhotoPostId];
		[self openMHomepyPhotoURLWithConfirmMessage:@"Mini Homepy photo post succeeded!!\nOpen the posted photo?"];
	}


	else if (kNateTagPostToCLogNote_uploadPhoto == nateTag)
	{
		NSString* attachId = [((NSDictionary*)result) objectForKey:@"attachId"];
		if ([attachId isEqualToString:@"-1"])
		{
			[self showAlertView:@"CLOG photo post failed!!"];
			return;
		}
		[self postMessageToCLogNoteWithAttachId:attachId];
	}
	else if (kNateTagPostToCLogNote_postMessage == nateTag)
	{
		NSDictionary* resultDic = (NSDictionary*)result;
		NSInteger errorCode = [[[resultDic objectForKey:@"rcode"] substringFromIndex:3] intValue];
		if (errorCode != 0)
		{
			[self showAlertView:[NSString stringWithFormat:@"CLOG photo post failed!!!\n%@",
								 [resultDic objectForKey:@"rmsg"]]];
			return;
		}

		[self setAttribute:[resultDic objectForKey:@"noteSeq"] forKey:kAttributeKeyCLogNoteSeq];
		[self openCLogNoteURLWithConfirmMessage:@"CLOG photo post succeeded!!\nOpen the posted photo?"];
	}
	else
	{
		CSAssert1(FALSE, @"unsupported nate tag [%d]", nateTag);
	}
}



- (void)nateRequest:(CSNateRequest*)request
	 didFailWithTag:(NSUInteger)nateTag
		withMessage:(NSString*)alertMessage
		  withError:(NSError*)error
{
	if (DEBUG) CSLog(@"===> %@", error);
	[self showAlertView:alertMessage];
}

- (void)nateRequest:(CSNateRequest*)request
   didFinishWithTag:(NSUInteger)nateTag
{
	[self stopProgressIndicator];
	if (m_nateRequest != nil)
	{
		[m_nateRequest release];
		m_nateRequest = nil;
	}
}




#pragma mark -
#pragma mark DemoViewController (PrivateMethods)


- (void)updateUI
{
	if ([self.appDelegate isNateAuthorized])
	{
		m_statusLabel.text = [NSString stringWithFormat:@"user : %@",
							  [self.appDelegate.nateOAuthResult nateOnProfileForKey:@"id"]];
		[m_loginButton setTitle:@"Nate Logout" forState:UIControlStateNormal];
	}
	else
	{
		m_statusLabel.text = @"not logined";
		[m_loginButton setTitle:@"Nate/Cyworld Login" forState:UIControlStateNormal];
	}
}


- (void)startProgressIndicator
{
	if ([m_progressIndicatorView isAnimating])
	{
		return;
	}
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

	m_progressIndicatorView.hidden = NO;
	[m_progressIndicatorView startAnimating];

	self.view.userInteractionEnabled = NO;
	self.navigationBarUserInteractionEnabled = NO;
}

- (void)stopProgressIndicator
{
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

	m_progressIndicatorView.hidden = YES;
	[m_progressIndicatorView stopAnimating];

	self.view.userInteractionEnabled = YES;
	self.navigationBarUserInteractionEnabled = YES;
}



#pragma mark -

- (void)loginNate
{
	if (m_nateSessionController != nil)
	{
		[m_nateSessionController release];
		m_nateSessionController = nil;
	}

	m_nateSessionController = [[CSNateSessionController alloc] init];
	m_nateSessionController.delegate = self;
	m_nateSessionController.nateTag = kNateTagLogin;
	[m_nateSessionController login];
}

- (void)logoutNate
{
	CSUIAlertView* alertView = [[[CSUIAlertView alloc]
								   initConfirmWithTitle:nil
								   message:@"logout from the nate?"
								   target:self
								   action:@selector(nateLogoutAfterConfirmView:buttonAtIndex:)] autorelease];
	[alertView show];
}

- (void)forceLogoutNate
{
	self.appDelegate.nateOAuthResult = nil;
	[self updateUI];
}

- (void)nateLogoutAfterConfirmView:(CSUIAlertView*)alertView buttonAtIndex:(NSUInteger)buttonIndex
{
	if (buttonIndex == 1)
	{
		[self forceLogoutNate];
	}
}





#pragma mark -

- (void)presentMHomepyPhotoFolderSelectView
{
	if ([self.appDelegate isNateAuthorized] == NO)
	{
		if (m_nateSessionController != nil)
		{
			[m_nateSessionController release];
			m_nateSessionController = nil;
		}
		m_nateSessionController = [[CSNateSessionController alloc] init];
		m_nateSessionController.delegate = self;
		m_nateSessionController.nateTag = kNateTagPostToMHomepyPhotoAlbum_selectFolder;
		[m_nateSessionController login];
		return;
	}

	CSNateMHomepyFolderSelectViewController* folderSelectViewController =
	[[CSNateMHomepyFolderSelectViewController alloc] initWithNateOAuthResult:self.appDelegate.nateOAuthResult
																 andDelegate:self
																 andMenuType:CSNateMHomepyMenuTypePhotoAlbum];

	folderSelectViewController.modalInPopover = YES;
	folderSelectViewController.navigationItem.title = @"Select a Folder";

	UINavigationController* navController = [[UINavigationController alloc]
											 initWithRootViewController:folderSelectViewController];
	[folderSelectViewController release];

	[self.navigationController presentModalViewController:navController animated:YES];
	[navController release];
}



- (void)confirmPostToMHomepyPhotoAlbum
{
	CSUIAlertView* alertView = [[[CSUIAlertView alloc]
								 initConfirmWithTitle:nil
								 message:@"post photo to MiniHomepy PhotoAlbum?"
								 target:self
								 action:@selector(uploadPhotoToMHomepyPhotoAlbumAfterConfirmView:buttonAtIndex:)] autorelease];
	[alertView show];
}

- (void)uploadPhotoToMHomepyPhotoAlbumAfterConfirmView:(CSUIAlertView*)alertView
										 buttonAtIndex:(NSUInteger)buttonIndex
{
	if (buttonIndex == 0)
	{
		[self removeAttributeForKey:kAttributeKeyMHomepyId];
		[self removeAttributeForKey:kAttributeKeyMHomepyFolderNo];
		return;
	}

	[self uploadPhotoToMHomepyPhotoAlbum];
}


- (void)uploadPhotoToMHomepyPhotoAlbum
{
	if (m_nateRequest != nil)
	{
		[m_nateRequest release];
		m_nateRequest = nil;
	}

	m_nateRequest = [[CSNateRequest alloc] initWithNateOAuthResult:self.appDelegate.nateOAuthResult];
	m_nateRequest.nateTag = kNateTagPostToMHomepyPhotoAlbum_uploadPhoto;
	m_nateRequest.delegate = self;

	//NSData* aPhotoData = UIImageJPEGRepresentation([UIImage imageNamed:@"test_photo.jpg"], 1.0);
	NSData* aPhotoData = UIImagePNGRepresentation([UIImage imageNamed:@"test_photo.png"]);
	CSMultipartAttach* aPhotoAttach = [[CSMultipartAttach alloc] initWithAttachData:aPhotoData
																		andFileName:@"test_photo.png"];

	NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
	[params setObject:aPhotoAttach forKey:@"file"];
	[aPhotoAttach release];

	[m_nateRequest multipartRequestWithMethodName:CSNateRequestUploadPhoto
										andParams:[params autorelease]];
}


- (void)postMessageToMHomepyPhotoAlbumWithAttachName:(NSString*)attachName
{
	if (m_nateRequest != nil)
	{
		[m_nateRequest release];
		m_nateRequest = nil;
	}

	m_nateRequest = [[CSNateRequest alloc] initWithNateOAuthResult:self.appDelegate.nateOAuthResult];
	m_nateRequest.nateTag = kNateTagPostToMHomepyPhotoAlbum_postMessage;
	m_nateRequest.delegate = self;

	NSString* demoContents = @"케른스토리(CairnStory)는\n"
							"모바일 앱을 통해 나의 여행기록을 남기고\n"
							"웹사이트를 통해 편리하게 관리하고 다른 사람들과 공유할 수 있으며,\n"
							"SNS를 통해 친구들에게 내 여행경험을 알릴 수 있습니다.\n\n"
							"<a href=\"http://www.cairnstory.com\">www.CairnStory.com</a>\n"
							"<a href=\"http://itunes.apple.com/kr/app/id406475979?mt=8\">Apple App Store</a>\n"
							"<a href=\"https://market.android.com/details?id=com.sngsoft.android.cairnstory\">Google Android Market</a>\n"
							"<a href=\"http://market.olleh.com/appDetail?ptype=C&pid=51200002280660&LNBUrl=\">KT Olleh Market</a>\n";

	NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
	[params setObject:[self attributeForKey:kAttributeKeyMHomepyFolderNo] forKey:@"folderNo"];
	[params setObject:@"CairnStory" forKey:@"title"];
	[params setObject:demoContents forKey:@"content"];
	[params setObject:@"4" forKey:@"itemOpen"];
	[params setObject:attachName forKey:@"photoUrls"];
	//[params setObject:@"false" forKey:@"isScrapOpen"];
	//[params setObject:@"false" forKey:@"isSearchOpen"];

	[m_nateRequest requestWithMethodName:CSNateRequestRegisterPhotoMessage
							   andParams:[params autorelease]];
}


- (void)openMHomepyPhotoURLWithConfirmMessage:(NSString*)confirmMessage
{
	CSUIAlertView* alertView = [[[CSUIAlertView alloc]
								 initConfirmWithTitle:nil
								 message:confirmMessage
								 target:self
								 action:@selector(openMHomepyPhotoURLAfterConfirmView:buttonAtIndex:)] autorelease];

	[alertView show];
}


- (void)openMHomepyPhotoURLAfterConfirmView:(CSUIAlertView*)alertView
							  buttonAtIndex:(NSUInteger)buttonIndex
{
	if (buttonIndex == 0)
	{
		[self removeAttributeForKey:kAttributeKeyMHomepyId];
		[self removeAttributeForKey:kAttributeKeyMHomepyFolderNo];
		[self removeAttributeForKey:kAttributeKeyMHomepyPhotoPostId];
		return;
	}

//	NSString* urlString = [NSString stringWithFormat:@"http://minihp.cyworld.com/pims/main/pims_main.asp"
//						   "?urlstr=phot"
//						   "&tid=%@"
//						   "&folder_part=%@"
//						   "&item_seq=%@&board_no=3&urlstrsub=search",
//						   [self attributeForKey:kAttributeKeyMHomepyId],
//						   [self attributeForKey:kAttributeKeyMHomepyFolderNo],
//						   [self attributeForKey:kAttributeKeyMHomepyPhotoPostId]];


	NSString* urlString = [NSString stringWithFormat:@"http://m.minihp.cyworld.nate.com/minihp/photoView.php"
						   "?hp_id=%@"
						   "&bo_no=%@"
						   "&board_no=0&frompage=1&fromvt=thumb",
						   [self attributeForKey:kAttributeKeyMHomepyId],
						   [self attributeForKey:kAttributeKeyMHomepyPhotoPostId]];

	if (DEBUG) CSLog(@"===> %@", urlString);
	NSURL* aURL = [[NSURL alloc] initWithString:urlString];

	[self removeAttributeForKey:kAttributeKeyMHomepyId];
	[self removeAttributeForKey:kAttributeKeyMHomepyFolderNo];
	[self removeAttributeForKey:kAttributeKeyMHomepyPhotoPostId];

	[[UIApplication sharedApplication] openURL:[aURL autorelease]];
}



#pragma mark -

- (void)confirmPostToCLogNote
{
	if ([self.appDelegate isNateAuthorized] == NO)
	{
		if (m_nateSessionController != nil)
		{
			[m_nateSessionController release];
			m_nateSessionController = nil;
		}
		m_nateSessionController = [[CSNateSessionController alloc] init];
		m_nateSessionController.delegate = self;
		m_nateSessionController.nateTag = kNateTagPostToCLogNote_uploadPhoto;
		[m_nateSessionController login];
		return;
	}

	if ([self.appDelegate.nateOAuthResult hasCLogProfile] == NO)
	{
		[self showAlertView:@"not login to CLOG"];
		return;
	}

	CSUIAlertView* alertView = [[[CSUIAlertView alloc]
								 initConfirmWithTitle:nil
								 message:@"post photo to CLOG note?"
								 target:self
								 action:@selector(uploadToCLogAfterConfirmView:buttonAtIndex:)] autorelease];
	[alertView show];
}

- (void)uploadToCLogAfterConfirmView:(CSUIAlertView*)alertView buttonAtIndex:(NSUInteger)buttonIndex
{
	if (buttonIndex == 0)
	{
		return;
	}

	[self uploadPhotoToCLogNote];
}

- (void)uploadPhotoToCLogNote
{
	if (m_nateRequest != nil)
	{
		[m_nateRequest release];
		m_nateRequest = nil;
	}

	m_nateRequest = [[CSNateRequest alloc] initWithNateOAuthResult:self.appDelegate.nateOAuthResult];
	m_nateRequest.nateTag = kNateTagPostToCLogNote_uploadPhoto;
	m_nateRequest.delegate = self;

	//NSData* aPhotoData = UIImageJPEGRepresentation([UIImage imageNamed:@"test_photo.png"], 1.0);
	NSData* aPhotoData = UIImagePNGRepresentation([UIImage imageNamed:@"test_photo.png"]);
	CSMultipartAttach* aPhotoAttach = [[CSMultipartAttach alloc] initWithAttachData:aPhotoData
																		andFileName:@"test_photo.png"];

	NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
	[params setObject:@"json" forKey:@"output"];
	[params setObject:aPhotoAttach forKey:@"image"];
	[aPhotoAttach release];

	[m_nateRequest multipartRequestWithMethodName:CSNateRequestCLogUploadPhoto
										andParams:[params autorelease]
									andOutputType:CSNateRequestOutputTypeJSON];
}


- (void)postMessageToCLogNoteWithAttachId:(NSString*)attachId
{
	if (m_nateRequest != nil)
	{
		[m_nateRequest release];
		m_nateRequest = nil;
	}

	m_nateRequest = [[CSNateRequest alloc] initWithNateOAuthResult:self.appDelegate.nateOAuthResult];
	m_nateRequest.nateTag = kNateTagPostToCLogNote_postMessage;
	m_nateRequest.delegate = self;

	NSString* demoContents = @"케른스토리(CairnStory)는\n"
							"모바일 앱을 통해 나의 여행기록을 남기고\n"
							"웹사이트를 통해 편리하게 관리하고 다른 사람들과 공유할 수 있으며,\n"
							"SNS를 통해 친구들에게 내 여행경험을 알릴 수 있습니다.\n\n"
							"CairnStory - http://www.cairnstory.com\n"
							"Apple App Store - http://itunes.apple.com/kr/app/id406475979?mt=8\n"
							"Google Android Market - https://market.android.com/details?id=com.sngsoft.android.cairnstory\n"
							"KT Olleh Market - http://market.olleh.com/appDetail?ptype=C&pid=51200002280660&LNBUrl=\n";

	NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
	[params setObject:@"json" forKey:@"output"];
	[params setObject:@"4" forKey:@"openType"];
	[params setObject:demoContents forKey:@"contents"];
	[params setObject:@"0" forKey:@"sendto"];
	[params setObject:@"1" forKey:@"deviceType"];
	if (attachId != nil)
	{
		[params setObject:attachId forKey:@"attachId"];
		[params setObject:@"1" forKey:@"attachType"];
	}

	[m_nateRequest requestWithMethodName:CSNateRequestCLogAddNote
							   andParams:[params autorelease]
						   andOutputType:CSNateRequestOutputTypeJSON];
}


- (void)openCLogNoteURLWithConfirmMessage:(NSString*)confirmMessage
{
	CSUIAlertView* alertView = [[[CSUIAlertView alloc]
								 initConfirmWithTitle:nil
								 message:confirmMessage
								 target:self
								 action:@selector(openCLogNoteURLAfterConfirmView:buttonAtIndex:)] autorelease];

	[alertView show];
}

- (void)openCLogNoteURLAfterConfirmView:(CSUIAlertView*)alertView
						  buttonAtIndex:(NSUInteger)buttonIndex
{
	if (buttonIndex == 0)
	{
		[self removeAttributeForKey:kAttributeKeyCLogNoteSeq];
		return;
	}

//	NSString* urlString = [NSString stringWithFormat:@"http://c.cyworld.com/%@/note/%@",
//						   [self.appDelegate.nateOAuthResult cLogProfileForKey:@"id"],
//						   [self attributeForKey:kAttributeKeyCLogNoteSeq]];

	NSString* urlString = [NSString stringWithFormat:@"http://m.minihp.cyworld.nate.com/clog/noteView.php"
						   "?hp_id=%@&itemSeq=%@",
						   [self.appDelegate.nateOAuthResult cLogProfileForKey:@"id"],
						   [self attributeForKey:kAttributeKeyCLogNoteSeq]];

	if (DEBUG) CSLog(@"===> %@", urlString);
	NSURL* aURL = [[NSURL alloc] initWithString:urlString];
	[self removeAttributeForKey:kAttributeKeyCLogNoteSeq];

	[[UIApplication sharedApplication] openURL:[aURL autorelease]];
}


@end


//
//	 미니홈피 사진 업로드 결과
//	Data =     {
//		Attach =         {
//			AttachNm =             {
//				"#text" = "/340007/2011/4/5/15/test_photo.png";
//			};
//			AttachSize =             {
//				"#text" = 174613;
//			};
//			Extension =             {
//				"#text" = jpg;
//			};
//			FileName =             {
//				"#text" = "test_photo.png";
//			};
//			MimeType =             {
//				"#text" = "image/jpeg";
//			};
//		};
//	};
//
//
//	 미니홈피 사진글 작성 결과
//	ArrayOfint =     {
//		"#attributes" =         {
//			xmlns = "http://schemas.microsoft.com/2003/10/Serialization/Arrays";
//			"xmlns:i" = "http://www.w3.org/2001/XMLSchema-instance";
//		};
//		int =         {
//			"#text" = 517803288;
//		};
//	};



//	CLog 사진 업로드 결과
//	{
//		attachId = cc13c27a1bf036e133d5ce777aafe47e59e9b3d906a0d7797f223619ee674c5f34ddcd89655cdffbbeded3672a5b39450482ee64ac4339828e35ef69f5e16d37;
//		rcode = RET0000;
//		rmsg = success;
//	}

//	CLog 노트글 작성 결과
//	{
//		noteSeq = 62723;
//		rcode = RET0000;
//		rmsg = success;
//	}
